package com.sharedlease.websocket;

import com.sharedlease.entity.Chat;
import com.sharedlease.entity.Message;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;
import java.util.concurrent.TimeUnit;

public class ScheduleUtil {

    public static void sendRefundMessage(Chat message) throws SchedulerException, InterruptedException{
        StdSchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();
        JobDetail jobDetail = JobBuilder.newJob(SingleMessageJob.class).withIdentity("job_rend" + message.getId(), "group_refund").build();
        scheduler.getContext().put("message", message);
        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger_rend" + message.getId(), "trigger_refund")
                .startNow()
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                        .withIntervalInSeconds(1)
                        ).build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
        TimeUnit.SECONDS.sleep(1);
        scheduler.shutdown();
    }



    public static void sendTimeRent(Date date, Chat chat) throws SchedulerException, InterruptedException{
        StdSchedulerFactory factory = new StdSchedulerFactory();
        Scheduler scheduler = factory.getScheduler();

        JobDetail jobDetail = JobBuilder.newJob(SingleMessageJob.class).withIdentity("job_rent" + chat.getDestId(), "group_rent").build();
        scheduler.getContext().put("message", chat);
        SimpleTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger_rent" + chat.getDestId(), "trigger_rent")
                .startAt(date)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule()
                ).build();
        scheduler.scheduleJob(jobDetail, trigger);
        scheduler.start();
    }
}
